﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using App1AppHandler;
using Reborn_WorldServer;

namespace Reborn_Server.App1AppHandler
{
    public class Spell_Stun : Buff
    {
        //private CreatureGroup oldCreatureGroup;
        public Spell_Stun() 
        {
            spellTimer.Interval = 2000;
            minutesLeft = -2;
            
            //Rank 1
            ticksLeft = 4;
        }

        override public void Resume(CreatureBase targetCreatureBase, SpellIcon _spellIcon, int timeRemaining, int castLevel, int castCharacterID, int castInt, int castWis, int optionalValue, string xmlData)
        {
            if (timeRemaining == -1)
            {
                if (_spellIcon.Name.IndexOf("Rank 2") > -1 || _spellIcon.Name.IndexOf("Stunning Shout") > -1)
                    timeRemaining = 5;
                else
                    timeRemaining = 4;
            }

            ticksLeft = timeRemaining;
            base.Resume(targetCreatureBase, _spellIcon, timeRemaining, castLevel, castCharacterID, castInt, castWis, optionalValue, xmlData);

            //custom spell logic here
            if (TargetCreatureBase is Character)
            {
                Character targetCharacter = (Character)TargetCreatureBase;
                targetCharacter.SetBusy(true);

                Message msg = new Message();
                msg.CreatureID = targetCharacter.CreatureID;
                msg.Text = "You are stunned!";
                msg.Type = "damaged";

                targetCharacter.AddMessage(msg);
            }

            spellTimer.Start();
        }

        public override bool tryResist(SpellIcon _spellIcon, int castLevel, int castInt, int castWis, CreatureBase targetCreatureBase)
        {
            int penetration = SkillManager.AdjustForCombat(castLevel, 0, castInt);

            int resist = 0;
            if (_spellIcon.Name.IndexOf("Rank 2") > -1 || _spellIcon.Name == "Stunning Shout")
                resist = System.Convert.ToInt32(targetCreatureBase.getResist(_spellIcon.Skill) * 1.5);
            else
                resist = targetCreatureBase.getResist(_spellIcon.Skill) * 2;

            int result = Managers.GameHandler.random.Next(1, penetration + resist + 1);
            if (result > penetration)
                return true;

            return false;
        }

        public void Cast(CreatureBase myCreatureBase, int CastLevel, long CreatureGUID)
        {
            List<MapItem> MapItemsWithin12;
            if (base.InitializeCast(myCreatureBase, CastLevel, CreatureGUID, true, out MapItemsWithin12) == false)
                return;

            if (spellIcon.Name.IndexOf("Rank 2") > -1)
                ticksLeft = 5;
            else
                ticksLeft = 4;

            myCreatureBase.Update_Character();
            TargetCreatureBase.buffManager.UpdateBuffs();

            //custom spell logic here
            if (TargetCreatureBase is Character)
            {
                Character targetCharacter = (Character)TargetCreatureBase;
                targetCharacter.SetBusy(true);

                Message msg = new Message();
                msg.CreatureID = targetCharacter.CreatureID;
                msg.Text = "You are stunned!";
                msg.Type = "damaged";

                targetCharacter.AddMessage(msg);
            }

            Message msg1 = new Message();
            msg1.CreatureID = TargetCreatureBase.CreatureID;
            msg1.Text = TargetCreatureBase.Name + " is stunned!";
            msg1.Type = "spelloffense";

            TargetCreatureBase.MessageBroadcast(MapItemsWithin12, msg1, false);


            spellTimer.Start();
        }

        public override void RemoveSpell(bool updateBuffs, bool sendMessage)
        {
            base.RemoveSpell(updateBuffs, sendMessage);
            if (TargetCreatureBase is Character)
            {
                Character targetCharacter = (Character)TargetCreatureBase;
                targetCharacter.SetBusy(false);
            }
        }

        protected override void SpellTimer_Expired(object source, System.Timers.ElapsedEventArgs e)
        {
            
            LockItem lockitem = TargetCreatureBase.GameHandler.GetLockItem(TargetCreatureBase.MapID, TargetCreatureBase.Instance);

            lock (lockitem.LockObj)
            {
                //This buff has been removed, do nothing (this can occur if the buff gets overwritten exactly when this timer fires)
                if (TargetCreatureBase.buffManager.hasBuff(this.SpellID) == false)
                    return;

                if (ticksLeft == 0)
                {
                    RemoveSpell(true, true);
                    return;
                }

                ticksLeft--;

                if (TargetCreatureBase is Character)
                {
                    Character targetCharacter = (Character)TargetCreatureBase;

                    Message msg = new Message();
                    msg.CreatureID = targetCharacter.CreatureID;
                    msg.Text = "You are stunned!";
                    msg.Type = "damaged";

                    targetCharacter.AddMessage(msg);
                }
                base.spellTimer.Start();
            }
        }
    }
}
